package com.example.controller;

import com.example.annotation.AsyncMessageHandler;
import com.example.entity.Dish;
import com.example.service.DishService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
@Tag(name = "菜谱管理")
public class DishController {
    
    @Autowired
    private DishService dishService;
    
    @GetMapping("/list")
    @Operation(summary = "获取所有菜品列表")
    public List<Dish> listAll() {
        return dishService.listAll();
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取菜品详情")
    public Dish getById(@PathVariable Long id) {
        return dishService.getById(id);
    }
    
    @PostMapping
    @Operation(summary = "新增菜品")
    @PreAuthorize("hasRole('ADMIN') or hasRole('SUPER_ADMIN')")
    @AsyncMessageHandler(queue = "dish.create.queue")
    public boolean saveDish(@RequestBody Dish dish) {
        return dishService.saveDish(dish);
    }
    
    @PutMapping
    @Operation(summary = "修改菜品")
    @PreAuthorize("hasRole('ADMIN') or hasRole('SUPER_ADMIN')")
    @AsyncMessageHandler(queue = "dish.update.queue")
    public boolean updateDish(@RequestBody Dish dish) {
        return dishService.updateDish(dish);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除菜品")
    @PreAuthorize("hasRole('ADMIN') or hasRole('SUPER_ADMIN')")
    @AsyncMessageHandler(queue = "dish.delete.queue")
    public boolean deleteDish(@PathVariable Long id) {
        return dishService.deleteDish(id);
    }

    @PutMapping("/{id}/status")
    public boolean updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        return dishService.updateStatus(id, status);
    }
} 